<template>
	<view>
		<dialog-shell ref="shell" title="用户登录" confirm-text="登录" @confirm="confirmCommit">
			<image src="../../static/icons/icon.png" class="icon-login"></image>
			<text class="info-welcome">WELCOME</text>
			<text class="info-register" @click="clickRegister">没有账号？请注册</text>
			<view class="login-container">
				<input type="text" :value="userName" placeholder="请输入用户名" placeholder-class="input-holder" @input="inputGetUser"/>
				<text class="iconfont icon-ic_user icon-wode"></text>
			</view>
			<view class="login-container">
				<input v-if="isPwd" type="password" :value="password" placeholder="请输入密码" placeholder-class="input-holder" @input="inputGetPwd"/>
				<input v-else type="text" :value="password" placeholder="请输入密码" placeholder-class="input-holder" @input="inputGetPwd"/>
				<text :class="isPwd ? 'iconfont icon-jinzhichakan ic-pwd' : 'iconfont icon-a-ziyuan563ldpi ic-pwd'" @click="clickSwitch"></text>
			</view>
		</dialog-shell>
	</view>
</template>

<script>
	export default {
		name:"login-dailog",
		data() {
			return {
				isPwd: true,
				password:"",
				userName:""
			};
		},
		mounted() {
			let userinfo = uni.getStorageSync("userinfo")
			if(userinfo){
				this.userName = userinfo.username
				this.password = userinfo.password
			}
		},
		methods: {
			confirmCommit(){
			    if(this.userName.length === 0 || this.password.length === 0){
			        uni.showTabBar({
			            title: "请输入用户名密码!"
			        });
			        return;
			    }
			    let url = this.$params.host + this.$params.action_login;
			    let data = {
			        username: this.userName,
			        password: this.password
			    };
			    uni.showLoading({
			        title: '登录中...',
			        mask: false
			    });
			    this.$req.post(url, data, res => {
			        //console.log(res);
			    
			        uni.showToast({
			            title: res.data.message,
			            icon: 'none'
			        });
			        if(!res.data.success){
			            this.$refs.shell.show();
			            return;
			        }
			        let app = getApp();
			        let json = res.data.data;
			        app.globalData.avatar = json.avatar;
			        app.globalData.uid = json.id;
			        app.globalData.nickName = json.nickName;
			        app.globalData.userName = json.userName;
			        app.globalData.token = json.token;
			        app.globalData.email = json.email;
			        app.globalData.type = json.type;
					this.$noti.post(this.$params.noti_login_status)
					uni.setStorageSync("userinfo", data)
			         /* console.log(app.globalData); */
					this.$emit("after")
			    }, () => {
			        uni.hideLoading();
			    });
			},
			clickRegister(){
			    uni.navigateTo({
			        url:'/pages/register/register',
			        success: res => {
			            this.$refs.shell.hide()
			        },
			        fail: () => {},
			        complete: () => {}
			    });
			},			
			clickSwitch(){
				this.isPwd = !this.isPwd
			},
			inputGetPwd(e){
				this.password = e.detail.value
			},
			inputGetUser(e){
				this.userName = e.detail.value
			},
			show(){
				this.$refs.shell.show()
			},
			hide(){
				this.$refs.shell.clickHide()
			}
		}
	}
</script>

<style>
@import url("login-dailog.css");
</style>